package com.jml.spring.factory;

import com.jml.spring.model.BeanDefinition;
import com.jml.spring.registry.DefaultSingletonBeanRegistry;

/**
 * 定义获取bean的流程
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory{

    @Override
    public Object getBean(String name) {
        Object bean = getSingleton(name);
        if (bean != null){
            return bean;
        }
        BeanDefinition bd = getBeanDefinition(name);
        if (bd == null){
            return null;
        }
        if (bd.isSingleton()){
            bean = createBean(bd);
            addSingleton(name,bean);
        }else if (bd.isPrototype()){
            bean = createBean(bd);
        }
        return bean;
    }

    /**
     * 延迟到DefaultListableBeanFactory区实现该方法
     * @param name
     * @return
     */
    protected abstract BeanDefinition getBeanDefinition(String name);

    /**
     * 延迟到AbstractAutowireCapableBeanFactory区实现该方法
     * @param bd
     * @return
     */
    protected abstract Object createBean(BeanDefinition bd);

}
